package com.example.cd5160866.materialdesgin;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import utils.utils.NotificationUtil;

public class FirstActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_first_activity);
    }

    public void onClickTest(View view) {
        switch (view.getId()) {
            case R.id.bt1:
                NotificationUtil.simple(getApplicationContext());
                break;
            case R.id.bt2:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        NotificationUtil.simple(getApplicationContext());
                    }
                }, 3000);
                break;
            case R.id.bt3:
                Toast.makeText(getApplicationContext(), "开始进度", Toast.LENGTH_SHORT).show();
                progress = 0;
                isIndeterminate = false;
                handler.post(runnable);
                break;
            case R.id.bt4:
                Toast.makeText(getApplicationContext(), "开始进度", Toast.LENGTH_SHORT).show();
                progress = 30;
                NotificationUtil.progress(getApplicationContext(), progress, true);
                isIndeterminate = true;
                handler.post(runnable);
                break;
            case R.id.bt5:
                NotificationUtil.simpleActivity(getApplicationContext());
                break;
            case R.id.bt6:
                NotificationUtil.simpleService(getApplicationContext());
                break;
        }
    }

    private int progress = 0;

    private boolean isIndeterminate = false;

    private Handler handler = new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            progress = progress + 2;
            if (!isIndeterminate)
                NotificationUtil.progress(getApplicationContext(), progress, false);

            if (progress >= 100) {
                handler.removeCallbacks(runnable);
                NotificationUtil.closeProgress(getApplicationContext());
                Toast.makeText(getApplicationContext(), "进度完成", Toast.LENGTH_SHORT).show();
                return;
            }
            handler.postDelayed(runnable, 100);
        }
    };
}
